import tkinter as tk
from tkinter import ttk
from typing import Callable


class ControlPanel:
    """控制面板组件：处理用户输入和核心操作触发"""

    def __init__(self, parent_frame: ttk.Frame, on_connect: Callable, on_add_monitor: Callable):
        self.frame = ttk.Frame(parent_frame)
        self.frame.pack(fill=tk.X, padx=10, pady=5)

        self.on_connect = on_connect  # 连接行情回调
        self.on_add_monitor = on_add_monitor  # 添加监控回调
        self._setup_controls()

    def _setup_controls(self):
        """创建控制面板控件"""
        # Tushare Token输入
        ttk.Label(self.frame, text="Tushare Token:").grid(
            row=0, column=0, padx=5, pady=5, sticky=tk.W
        )
        self.ts_token = tk.StringVar()
        ttk.Entry(
            self.frame,
            textvariable=self.ts_token,
            width=30,
            show="*"
        ).grid(row=0, column=1, padx=5, pady=5, sticky=tk.W)

        # 连接按钮
        ttk.Button(
            self.frame,
            text="连接行情",
            command=self.on_connect
        ).grid(row=0, column=2, padx=5, pady=5, sticky=tk.W)

        # 股票代码输入
        ttk.Label(self.frame, text="股票代码:").grid(
            row=0, column=3, padx=5, pady=5, sticky=tk.W
        )
        self.stock_code = tk.StringVar(value="600036")
        ttk.Entry(
            self.frame,
            textvariable=self.stock_code,
            width=10
        ).grid(row=0, column=4, padx=5, pady=5, sticky=tk.W)

        # 添加监控按钮
        ttk.Button(
            self.frame,
            text="添加监控",
            command=self.on_add_monitor
        ).grid(row=0, column=5, padx=5, pady=5, sticky=tk.W)

        # 刷新间隔选择
        ttk.Label(self.frame, text="刷新间隔(秒):").grid(
            row=0, column=6, padx=5, pady=5, sticky=tk.W
        )
        self.refresh_interval = tk.IntVar(value=5)
        ttk.Combobox(
            self.frame,
            textvariable=self.refresh_interval,
            values=[1, 3, 5, 10, 15],
            width=5,
            state="readonly"
        ).grid(row=0, column=7, padx=5, pady=5, sticky=tk.W)

    def get_token(self) -> str:
        """获取输入的Tushare Token"""
        return self.ts_token.get().strip()

    def get_stock_code(self) -> str:
        """获取输入的股票代码"""
        return self.stock_code.get().strip()

    def get_refresh_interval(self) -> int:
        """获取选中的刷新间隔"""
        return self.refresh_interval.get()